package com.association.service.impl;

import com.association.constant.CodeEnum;
import com.association.mapper.MenuMapper;
import com.association.mapper.PageDataMapper;
import com.association.mapper.WebPageMapper;
import com.association.mapper.WebPageTypeMapper;
import com.association.model.PageDrawingBo;
import com.association.model.ResultResp;
import com.association.model.entity.*;
import com.association.model.vo.ComposingAndModules;
import com.association.model.vo.WebProjectVo;
import com.association.service.WebPageService;
import com.association.utils.FileUtil;
import com.association.utils.RequestTokenBeanLocal;
import com.association.utils.UploadUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.len.base.impl.BaseServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @author 18244
 */
@Service
public class WebPageServiceImpl extends BaseServiceImpl<WebPage, String> implements WebPageService {
    @Autowired
    WebPageTypeMapper webPageTypeMapper;

    @Autowired
    PageDataMapper pageDataMapper;

    @Resource
    MenuMapper menuMapper;

    @Resource
    WebPageMapper webPageMapper;


    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public ResultResp pages(WebPage webPage) {
        return new ResultResp(this.list(new QueryWrapper<WebPage>().eq("project_id", webPage.getProjectId()).eq("is_del", 0)));
    }

    @Override
    public ResultResp getList(WebPage webPage) {
        List<WebPageType> list = webPageTypeMapper.selectList(new QueryWrapper<>());
        list.forEach(res->{
            QueryWrapper<WebPage> webPageQueryWrapper = new QueryWrapper<>();
            webPageQueryWrapper.eq("project_id", webPage.getProjectId());
            webPageQueryWrapper.eq("type",res.getId());
            webPageQueryWrapper.eq("is_del", 0);
            res.setPages(this.list(webPageQueryWrapper));
        });
        return new ResultResp(list);
    }

    @Override
    public ResultResp getById(WebPage webPage) {
        if(webPage.getId()!=0){
            return new ResultResp(this.getOne(new QueryWrapper<WebPage>().eq("id",webPage.getId())));
        }
        return new ResultResp(CodeEnum.SERVER);
    }

    @Override
    public ResultResp getByDate(WebPage webPage) {
        return new ResultResp(pageDataMapper.selectList(new QueryWrapper<PageData>().eq("pageId",webPage.getId())));
    }

    @Override
    public ResultResp addData(PageData pageData) {
        try {
            pageData.setCreateTime(new Date());
            if(pageDataMapper.insert(pageData)<=0){
                return new ResultResp(CodeEnum.SERVER);
            }
        }catch (Exception e){
            logger.error("\"WebPageService\": \"addData\", \"result\": \"exception\", \"msg\": {}, \"Exception\": {}",  pageData, e.getMessage());
            return new ResultResp(CodeEnum.SERVER);
        }
        return new ResultResp(CodeEnum.SUCCESS);
    }

    @Override
    public ResultResp edit(WebPage webPage) {
        try {
            this.update(webPage, new QueryWrapper<WebPage>().eq("id", webPage.getId()));
        }catch (Exception e){
            logger.error("页面模板修改失败:"+e.getMessage());
            return new ResultResp(CodeEnum.SERVER);
        }
        return new ResultResp(CodeEnum.SUCCESS);
    }

    @Override
    public ResultResp add(WebPage webPage) {

        try {
            Integer pageId = webPageMapper.insert(webPage);
            String path = "page"+pageId;
            QueryWrapper<Menu> wrapper = new QueryWrapper<>();
            Menu menuOne = menuMapper.selectOne(wrapper);
            Menu menu = new Menu();
            menu.setTitle(webPageMapper.selectById(pageId).getName());
            menu.setComponent("association/"+path);
            menu.setPid(2);
            menu.setPath(path);
            menu.setType(1);
            if (menuOne!=null){
                menuMapper.update(menu,new QueryWrapper<Menu>().eq("id", menu.getId()));
            }else {
                menuMapper.insert(menu);
            }
        }catch (Exception e){
            logger.error("页面模板添加失败:"+e.getMessage());
            return new ResultResp(CodeEnum.SERVER);
        }
        return new ResultResp(CodeEnum.SUCCESS);
    }

    @Override
    public ResultResp delete(WebPage webPage) {
        try {
            this.removeById(webPage.getId());
        }catch (Exception e){
            logger.error("页面模板删除失败:"+e.getMessage());
            return new ResultResp(CodeEnum.SERVER);
        }
        return new ResultResp(CodeEnum.SUCCESS);
    }

    @Override
    public ResultResp composingShow(WebPage webPage) {
        try{
            return new ResultResp(this.getOne(new QueryWrapper<WebPage>().eq("id", webPage.getId())));
        }catch (Exception e){
            logger.error("组件保存失败："+e.getMessage());
            return new ResultResp(CodeEnum.SERVER);
        }
    }

    @Override
    public ResultResp composingUpdate(WebPage webPage) {
        try{
            this.update(webPage,new QueryWrapper<WebPage>().eq("id", webPage.getId()));
            return new ResultResp(CodeEnum.SUCCESS);
        }catch (Exception e){
            logger.error("组件保存失败："+e.getMessage());
            return new ResultResp(CodeEnum.SERVER);
        }
    }
    @Override
    public ResultResp saveDrawingList(PageDrawingBo pageDrawingBo) {

        String path = "page"+pageDrawingBo.getPageId()+".vue";
        FileUtil.string2File(pageDrawingBo.getVueJson(),path);
        try{
            FileUtil.string2File(pageDrawingBo.getVueJson(),path);
            return new ResultResp(CodeEnum.SUCCESS);
        }catch (Exception e){
            logger.error("模板保存失败："+e.getMessage());
            return new ResultResp(CodeEnum.SERVER);
        }
    }

}
